package genetic;

import functionTypes.ConstantSampling;
import functionTypes.Interpolation;
import functionTypes.SamplingFunction;
import functionTypes.OldFunction;
import tasks.Boiler;
import tasks.BoilerControlSystem;
import Abstracts.AbstractControlSystem;

public class Cellular {

	public AbstractControlSystem[][] field;
	public double[][] scores;
	public String task;
	public String type;
	public int fieldSize = 10;
	public int maxEvolutionCount = 100;
	public double mutationProbability = 0.05;

	public double midScores[];
	public double topScores[];

	public AbstractControlSystem bestIndividual;
	public double bestIndividualScore;

	public Boiler boiler;

	private int left(int a) {
		if (a == 0)
			return fieldSize - 1;
		return a;
	}

	private int right(int a) {
		if (a == fieldSize - 1)
			return 0;
		return a;
	}

	public Cellular(String task, String type, int fieldSize,
			int maxEvolutionCount, double mutationProbability) throws Exception {
		scores = new double[fieldSize][fieldSize];

		if (maxEvolutionCount > 2) {
			this.maxEvolutionCount = maxEvolutionCount;
		}
		midScores = new double[maxEvolutionCount];
		topScores = new double[maxEvolutionCount];

		if ((mutationProbability > 0) && (mutationProbability < 1)) {
			this.mutationProbability = mutationProbability;
		}

		if (!(type.equals("old") || type.equals("sampling")
				|| type.equals("constantSampling") || type
				.equals("interpolation"))) {
			throw new Exception(
					"Types may be: old, sampling, constantSampling, interpolation");
		}

		if (task.equals("boiler")) {
			this.task = "boiler";
			this.type = type;
			if (fieldSize > 2) {
				this.fieldSize = fieldSize;
			}
			boiler = new Boiler();
			createBoilerField();
			if (type.equals("old")) {
				oldBoilerGenetic();
			} else if (type.equals("sampling")) {
				samplingBoilerGenetic();
			} else if (type.equals("constantSampling")) {
				constatntSamplingBoilerGenetic();
			} else if (type.equals("interpolation")) {
				interpolationBoilerGenetic();
			}
		}
	}

	private void interpolationBoilerGenetic() throws Exception {
		for (int i = 0; i < maxEvolutionCount; ++i) {
			System.out.println("\nEvolution step:" + i);
			for (int j = 0; j < fieldSize; ++j) {
				for (int k = 0; k < fieldSize; ++k) {

					if (mutationProbability > Math.random()) {
						field[j][k].generateControlSystem();
						continue;
					}

					Interpolation[] f1 = (Interpolation[]) field[j][k]
							.getFunctions();
					Interpolation[] f2 = (Interpolation[]) field[left(j)][k]
							.getFunctions();
					Interpolation[] f3 = (Interpolation[]) field[right(j)][k]
							.getFunctions();
					Interpolation[] f4 = (Interpolation[]) field[j][left(j)]
							.getFunctions();
					Interpolation[] f5 = (Interpolation[]) field[j][right(j)]
							.getFunctions();

					double myEff = boiler.getEffectivnes(field[j][k]);
					scores[j][k] = myEff;

					Interpolation[] newF1 = new Interpolation[f1.length];
					Interpolation[] newF2 = new Interpolation[f1.length];
					Interpolation[] newF3 = new Interpolation[f1.length];
					Interpolation[] newF4 = new Interpolation[f1.length];
					Interpolation[] newF5 = new Interpolation[f1.length];
					Interpolation[] newF6 = new Interpolation[f1.length];
					Interpolation[] newF7 = new Interpolation[f1.length];
					Interpolation[] newF8 = new Interpolation[f1.length];

					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						Interpolation[] crossResult = f1[fffuuu]
								.crossover(f2[fffuuu]);
						newF1[fffuuu] = crossResult[2];
						newF2[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						Interpolation[] crossResult = f1[fffuuu]
								.crossover(f3[fffuuu]);
						newF3[fffuuu] = crossResult[2];
						newF4[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						Interpolation[] crossResult = f1[fffuuu]
								.crossover(f4[fffuuu]);
						newF5[fffuuu] = crossResult[2];
						newF6[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						Interpolation[] crossResult = f1[fffuuu]
								.crossover(f5[fffuuu]);
						newF7[fffuuu] = crossResult[2];
						newF8[fffuuu] = crossResult[3];
					}

					BoilerControlSystem cs1 = new BoilerControlSystem(newF1);
					BoilerControlSystem cs2 = new BoilerControlSystem(newF2);
					BoilerControlSystem cs3 = new BoilerControlSystem(newF3);
					BoilerControlSystem cs4 = new BoilerControlSystem(newF4);
					BoilerControlSystem cs5 = new BoilerControlSystem(newF5);
					BoilerControlSystem cs6 = new BoilerControlSystem(newF6);
					BoilerControlSystem cs7 = new BoilerControlSystem(newF7);
					BoilerControlSystem cs8 = new BoilerControlSystem(newF8);

					BoilerControlSystem tmpCS = null;

					double eff = boiler.getEffectivnes(cs1);
					if (eff < myEff) {
						tmpCS = cs1;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs2);
					if (eff < myEff) {
						tmpCS = cs2;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs3);
					if (eff < myEff) {
						tmpCS = cs3;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs4);
					if (eff < myEff) {
						tmpCS = cs4;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs5);
					if (eff < myEff) {
						tmpCS = cs5;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs6);
					if (eff < myEff) {
						tmpCS = cs6;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs7);
					if (eff < myEff) {
						tmpCS = cs7;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs8);
					if (eff < myEff) {
						tmpCS = cs8;
						myEff = eff;
					}

					if (tmpCS != null) {
						field[j][k] = tmpCS;

						if (bestIndividual == null) {
							bestIndividual = tmpCS;
							bestIndividualScore = boiler.getEffectivnes(tmpCS);
							System.out.println("best:" + bestIndividualScore);
						} else {
							double score = boiler.getEffectivnes(tmpCS);
							if (bestIndividualScore > score) {
								bestIndividual = tmpCS;
								bestIndividualScore = boiler
										.getEffectivnes(tmpCS);
								System.out.println("best:"
										+ bestIndividualScore);
							}
						}
					}
				}
			}

			double midScore = 0;

			for (int j = 0; j != fieldSize; ++j) {
				for (int k = 0; k != fieldSize; ++k) {
					midScore += scores[j][k];
				}
			}
			midScore /= fieldSize * fieldSize;
			midScores[i] = midScore;
			topScores[i] = bestIndividualScore;
			System.out.println("Best score: " + bestIndividualScore);
			System.out.println("Mid score: " + midScore);
		}
		
	}

	public void createBoilerField() {
		field = new BoilerControlSystem[fieldSize][fieldSize];

		int innerType = 0;

		if (type.equals("old")) {
			innerType = 0;
		} else if (type.equals("sampling")) {
			innerType = 1;
		} else if (type.equals("interpolation")) {
			innerType = 2;
		}

		for (int i = 0; i != fieldSize; ++i) {
			for (int j = 0; j != fieldSize; ++j) {
				field[i][j] = new BoilerControlSystem();
				if (innerType == 0) {
					OldFunction[] functions = new OldFunction[field[i][j]
							.getDimensions()];
					for (int k = 0; k != functions.length; ++k) {
						functions[k] = new OldFunction();
						functions[k].generateFunction();
					}
					field[i][j].setControlSystem(functions);
				} else if (innerType == 1) {
					SamplingFunction[] functions = new SamplingFunction[field[i][j]
							.getDimensions()];
					for (int k = 0; k != functions.length; ++k) {
						functions[k] = new SamplingFunction();
						functions[k].generateFunction();
					}
					field[i][j].setControlSystem(functions);
				} else if (innerType == 2) {
					Interpolation[] functions = new Interpolation[field[i][j]
							.getDimensions()];
					for (int k = 0; k != functions.length; ++k) {
						functions[k] = new Interpolation();
						functions[k].generateFunction();
					}
					field[i][j].setControlSystem(functions);
				}
			}
		}
	}

	public void oldBoilerGenetic() {
		for (int i = 0; i < maxEvolutionCount; ++i) {
			System.out.println("\nEvolution step:" + i);
			for (int j = 0; j < fieldSize; ++j) {
				for (int k = 0; k < fieldSize; ++k) {

					if (mutationProbability > Math.random()) {
						field[j][k].generateControlSystem();
						continue;
					}

					OldFunction[] f1 = (OldFunction[]) field[j][k]
							.getFunctions();
					OldFunction[] f2 = (OldFunction[]) field[left(j)][k]
							.getFunctions();
					OldFunction[] f3 = (OldFunction[]) field[right(j)][k]
							.getFunctions();
					OldFunction[] f4 = (OldFunction[]) field[j][left(j)]
							.getFunctions();
					OldFunction[] f5 = (OldFunction[]) field[j][right(j)]
							.getFunctions();

					double myEff = boiler.getEffectivnes(field[j][k]);
					scores[j][k] = myEff;

					OldFunction[] newF1 = new OldFunction[f1.length];
					OldFunction[] newF2 = new OldFunction[f1.length];
					OldFunction[] newF3 = new OldFunction[f1.length];
					OldFunction[] newF4 = new OldFunction[f1.length];
					OldFunction[] newF5 = new OldFunction[f1.length];
					OldFunction[] newF6 = new OldFunction[f1.length];
					OldFunction[] newF7 = new OldFunction[f1.length];
					OldFunction[] newF8 = new OldFunction[f1.length];

					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						OldFunction[] crossResult = f1[fffuuu]
								.crossover(f2[fffuuu]);
						newF1[fffuuu] = crossResult[2];
						newF2[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						OldFunction[] crossResult = f1[fffuuu]
								.crossover(f3[fffuuu]);
						newF3[fffuuu] = crossResult[2];
						newF4[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						OldFunction[] crossResult = f1[fffuuu]
								.crossover(f4[fffuuu]);
						newF5[fffuuu] = crossResult[2];
						newF6[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						OldFunction[] crossResult = f1[fffuuu]
								.crossover(f5[fffuuu]);
						newF7[fffuuu] = crossResult[2];
						newF8[fffuuu] = crossResult[3];
					}

					BoilerControlSystem cs1 = new BoilerControlSystem(newF1);
					BoilerControlSystem cs2 = new BoilerControlSystem(newF2);
					BoilerControlSystem cs3 = new BoilerControlSystem(newF3);
					BoilerControlSystem cs4 = new BoilerControlSystem(newF4);
					BoilerControlSystem cs5 = new BoilerControlSystem(newF5);
					BoilerControlSystem cs6 = new BoilerControlSystem(newF6);
					BoilerControlSystem cs7 = new BoilerControlSystem(newF7);
					BoilerControlSystem cs8 = new BoilerControlSystem(newF8);

					BoilerControlSystem tmpCS = null;

					double eff = boiler.getEffectivnes(cs1);
					if (eff < myEff) {
						tmpCS = cs1;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs2);
					if (eff < myEff) {
						tmpCS = cs2;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs3);
					if (eff < myEff) {
						tmpCS = cs3;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs4);
					if (eff < myEff) {
						tmpCS = cs4;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs5);
					if (eff < myEff) {
						tmpCS = cs5;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs6);
					if (eff < myEff) {
						tmpCS = cs6;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs7);
					if (eff < myEff) {
						tmpCS = cs7;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs8);
					if (eff < myEff) {
						tmpCS = cs8;
						myEff = eff;
					}

					if (tmpCS != null) {
						field[j][k] = tmpCS;

						if (bestIndividual == null) {
							bestIndividual = tmpCS;
							bestIndividualScore = boiler.getEffectivnes(tmpCS);
							System.out.println("best:" + bestIndividualScore);
						} else {
							double score = boiler.getEffectivnes(tmpCS);
							if (bestIndividualScore > score) {
								bestIndividual = tmpCS;
								bestIndividualScore = boiler
										.getEffectivnes(tmpCS);
								System.out.println("best:"
										+ bestIndividualScore);
							}
						}
					}
				}
			}

			double midScore = 0;

			for (int j = 0; j != fieldSize; ++j) {
				for (int k = 0; k != fieldSize; ++k) {
					midScore += scores[j][k];
				}
			}
			midScore /= fieldSize * fieldSize;
			midScores[i] = midScore;
			topScores[i] = bestIndividualScore;
			System.out.println("Best score: " + bestIndividualScore);
			System.out.println("Mid score: " + midScore);
		}
	}

	public void samplingBoilerGenetic() throws Exception {
		for (int i = 0; i < maxEvolutionCount; ++i) {
			System.out.println("\nEvolution step:" + i);
			for (int j = 0; j < fieldSize; ++j) {
				for (int k = 0; k < fieldSize; ++k) {

					if (mutationProbability > Math.random()) {
						field[j][k].generateControlSystem();
						continue;
					}

					SamplingFunction[] f1 = (SamplingFunction[]) field[j][k]
							.getFunctions();
					SamplingFunction[] f2 = (SamplingFunction[]) field[left(j)][k]
							.getFunctions();
					SamplingFunction[] f3 = (SamplingFunction[]) field[right(j)][k]
							.getFunctions();
					SamplingFunction[] f4 = (SamplingFunction[]) field[j][left(j)]
							.getFunctions();
					SamplingFunction[] f5 = (SamplingFunction[]) field[j][right(j)]
							.getFunctions();

					double myEff = boiler.getEffectivnes(field[j][k]);
					scores[j][k] = myEff;

					SamplingFunction[] newF1 = new SamplingFunction[f1.length];
					SamplingFunction[] newF2 = new SamplingFunction[f1.length];
					SamplingFunction[] newF3 = new SamplingFunction[f1.length];
					SamplingFunction[] newF4 = new SamplingFunction[f1.length];
					SamplingFunction[] newF5 = new SamplingFunction[f1.length];
					SamplingFunction[] newF6 = new SamplingFunction[f1.length];
					SamplingFunction[] newF7 = new SamplingFunction[f1.length];
					SamplingFunction[] newF8 = new SamplingFunction[f1.length];

					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						SamplingFunction[] crossResult = f1[fffuuu]
								.crossover(f2[fffuuu]);
						newF1[fffuuu] = crossResult[2];
						newF2[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						SamplingFunction[] crossResult = f1[fffuuu]
								.crossover(f3[fffuuu]);
						newF3[fffuuu] = crossResult[2];
						newF4[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						SamplingFunction[] crossResult = f1[fffuuu]
								.crossover(f4[fffuuu]);
						newF5[fffuuu] = crossResult[2];
						newF6[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						SamplingFunction[] crossResult = f1[fffuuu]
								.crossover(f5[fffuuu]);
						newF7[fffuuu] = crossResult[2];
						newF8[fffuuu] = crossResult[3];
					}

					BoilerControlSystem cs1 = new BoilerControlSystem(newF1);
					BoilerControlSystem cs2 = new BoilerControlSystem(newF2);
					BoilerControlSystem cs3 = new BoilerControlSystem(newF3);
					BoilerControlSystem cs4 = new BoilerControlSystem(newF4);
					BoilerControlSystem cs5 = new BoilerControlSystem(newF5);
					BoilerControlSystem cs6 = new BoilerControlSystem(newF6);
					BoilerControlSystem cs7 = new BoilerControlSystem(newF7);
					BoilerControlSystem cs8 = new BoilerControlSystem(newF8);

					BoilerControlSystem tmpCS = null;

					double eff = boiler.getEffectivnes(cs1);
					if (eff < myEff) {
						tmpCS = cs1;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs2);
					if (eff < myEff) {
						tmpCS = cs2;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs3);
					if (eff < myEff) {
						tmpCS = cs3;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs4);
					if (eff < myEff) {
						tmpCS = cs4;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs5);
					if (eff < myEff) {
						tmpCS = cs5;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs6);
					if (eff < myEff) {
						tmpCS = cs6;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs7);
					if (eff < myEff) {
						tmpCS = cs7;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs8);
					if (eff < myEff) {
						tmpCS = cs8;
						myEff = eff;
					}

					if (tmpCS != null) {
						field[j][k] = tmpCS;

						if (bestIndividual == null) {
							bestIndividual = tmpCS;
							bestIndividualScore = boiler.getEffectivnes(tmpCS);
							System.out.println("best:" + bestIndividualScore);
						} else {
							double score = boiler.getEffectivnes(tmpCS);
							if (bestIndividualScore > score) {
								bestIndividual = tmpCS;
								bestIndividualScore = boiler
										.getEffectivnes(tmpCS);
								System.out.println("best:"
										+ bestIndividualScore);
							}
						}
					}
				}
			}

			double midScore = 0;

			for (int j = 0; j != fieldSize; ++j) {
				for (int k = 0; k != fieldSize; ++k) {
					midScore += scores[j][k];
				}
			}
			midScore /= fieldSize * fieldSize;
			midScores[i] = midScore;
			topScores[i] = bestIndividualScore;
			System.out.println("Best score: " + bestIndividualScore);
			System.out.println("Mid score: " + midScore);
		}
	}

	public void constatntSamplingBoilerGenetic() throws Exception {
		for (int i = 0; i < maxEvolutionCount; ++i) {
			System.out.println("\nEvolution step:" + i);
			for (int j = 0; j < fieldSize; ++j) {
				for (int k = 0; k < fieldSize; ++k) {

					if (mutationProbability > Math.random()) {
						field[j][k].generateControlSystem();
						continue;
					}

					ConstantSampling[] f1 = (ConstantSampling[]) field[j][k]
							.getFunctions();
					ConstantSampling[] f2 = (ConstantSampling[]) field[left(j)][k]
							.getFunctions();
					ConstantSampling[] f3 = (ConstantSampling[]) field[right(j)][k]
							.getFunctions();
					ConstantSampling[] f4 = (ConstantSampling[]) field[j][left(j)]
							.getFunctions();
					ConstantSampling[] f5 = (ConstantSampling[]) field[j][right(j)]
							.getFunctions();

					double myEff = boiler.getEffectivnes(field[j][k]);
					scores[j][k] = myEff;

					ConstantSampling[] newF1 = new ConstantSampling[f1.length];
					ConstantSampling[] newF2 = new ConstantSampling[f1.length];
					ConstantSampling[] newF3 = new ConstantSampling[f1.length];
					ConstantSampling[] newF4 = new ConstantSampling[f1.length];
					ConstantSampling[] newF5 = new ConstantSampling[f1.length];
					ConstantSampling[] newF6 = new ConstantSampling[f1.length];
					ConstantSampling[] newF7 = new ConstantSampling[f1.length];
					ConstantSampling[] newF8 = new ConstantSampling[f1.length];

					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						ConstantSampling[] crossResult = f1[fffuuu]
								.crossover(f2[fffuuu]);
						newF1[fffuuu] = crossResult[2];
						newF2[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						ConstantSampling[] crossResult = f1[fffuuu]
								.crossover(f3[fffuuu]);
						newF3[fffuuu] = crossResult[2];
						newF4[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						ConstantSampling[] crossResult = f1[fffuuu]
								.crossover(f4[fffuuu]);
						newF5[fffuuu] = crossResult[2];
						newF6[fffuuu] = crossResult[3];
					}
					for (int fffuuu = 0; fffuuu < f1.length; ++fffuuu) {
						ConstantSampling[] crossResult = f1[fffuuu]
								.crossover(f5[fffuuu]);
						newF7[fffuuu] = crossResult[2];
						newF8[fffuuu] = crossResult[3];
					}

					BoilerControlSystem cs1 = new BoilerControlSystem(newF1);
					BoilerControlSystem cs2 = new BoilerControlSystem(newF2);
					BoilerControlSystem cs3 = new BoilerControlSystem(newF3);
					BoilerControlSystem cs4 = new BoilerControlSystem(newF4);
					BoilerControlSystem cs5 = new BoilerControlSystem(newF5);
					BoilerControlSystem cs6 = new BoilerControlSystem(newF6);
					BoilerControlSystem cs7 = new BoilerControlSystem(newF7);
					BoilerControlSystem cs8 = new BoilerControlSystem(newF8);

					BoilerControlSystem tmpCS = null;

					double eff = boiler.getEffectivnes(cs1);
					if (eff < myEff) {
						tmpCS = cs1;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs2);
					if (eff < myEff) {
						tmpCS = cs2;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs3);
					if (eff < myEff) {
						tmpCS = cs3;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs4);
					if (eff < myEff) {
						tmpCS = cs4;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs5);
					if (eff < myEff) {
						tmpCS = cs5;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs6);
					if (eff < myEff) {
						tmpCS = cs6;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs7);
					if (eff < myEff) {
						tmpCS = cs7;
						myEff = eff;
					}
					eff = boiler.getEffectivnes(cs8);
					if (eff < myEff) {
						tmpCS = cs8;
						myEff = eff;
					}

					if (tmpCS != null) {
						field[j][k] = tmpCS;

						if (bestIndividual == null) {
							bestIndividual = tmpCS;
							bestIndividualScore = boiler.getEffectivnes(tmpCS);
							System.out.println("best:" + bestIndividualScore);
						} else {
							double score = boiler.getEffectivnes(tmpCS);
							if (bestIndividualScore > score) {
								bestIndividual = tmpCS;
								bestIndividualScore = boiler
										.getEffectivnes(tmpCS);
								System.out.println("best:"
										+ bestIndividualScore);
							}
						}
					}
				}
			}

			double midScore = 0;

			for (int j = 0; j != fieldSize; ++j) {
				for (int k = 0; k != fieldSize; ++k) {
					midScore += scores[j][k];
				}
			}
			midScore /= fieldSize * fieldSize;
			midScores[i] = midScore;
			topScores[i] = bestIndividualScore;
			System.out.println("Best score: " + bestIndividualScore);
			System.out.println("Mid score: " + midScore);
		}
	}

}
